﻿@model IEnumerable<CloudFranceTriple.Models.ForumPostSet>
@{
    if (ViewBag.SiteMapPath1.ToString() != String.Empty)
    {
        ViewBag.Title = String.Format("{0} > {1} > {2}", ViewBag.SiteMapPath1.ToString(), ViewBag.SiteMapPath2.ToString(), ViewBag.SiteMapPath3.ToString());
    }
    else
    {
        ViewBag.Title = "All posts.";
    }
    Layout = "~/Views/Shared/_Layout.cshtml";
    var db = new CloudFranceTriple.Models.cloudfrAboq2RsDMEntities();    
}
<div class="content-padding">
    <h1>@CloudFranceTriple.WebRole.Properties.Resources.ForumIndexTitle</h1>
    <br />
    <h5>@Html.ActionLink(ViewBag.SiteMapPath1 as string, "Index", "Forum", new { area = "Forum" }, null) > @Html.ActionLink(ViewBag.SiteMapPath2 as string, "Index", "Topic", new { area = "Forum", id = ViewBag.Category_Id as int? }, null) > @ViewBag.SiteMapPath3 > @ViewBag.LocalizedUserRights</h5>
    @Html.Action("ShowOnPost", "Annonce", new { area = "Forum" })
    <section>
        @Html.Partial("_TopicQueriesLinks")
        @{if ((ViewBag.ShowCreate as Boolean?) ?? false)
          {
            @Html.ActionLink(@CloudFranceTriple.WebRole.Properties.Resources.PostCreateButton, "Create", "Post", new { area = "Forum", id = ViewBag.Topic_Id as int? }, null)
          }
        }
        @Html.Action("PollItem", new { TopicId = ViewBag.Topic_Id as int? })
        <table style="width: 100%; border: 1px solid #fffffe;">
            @{foreach (var item in Model)
              {
                  string hash;
                  var messageAuthor = db.UserProfile.SingleOrDefault(user => user.UserName == item.PostMessage_Author);
                  if (messageAuthor != null)
                  {
                      var source = messageAuthor.gravatar.Trim().ToLower();
                      using (System.Security.Cryptography.MD5 md5Hash = System.Security.Cryptography.MD5.Create())
                      {
                          var data = md5Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(source));
                          var sBuilder = new System.Text.StringBuilder();
                          for (int i = 0; i < data.Length; i++)
                          {
                              sBuilder.Append(data[i].ToString("x2"));
                          }
                          hash = sBuilder.ToString();
                      }
                      ViewBag.GravatarImageAlt = messageAuthor.UserName;
                      ViewBag.GravatarImageUrl = String.Format("https://www.gravatar.com/avatar/{0}?s=120&d=http://s19.postimage.org/62du01yov/default_avatar_82.png&r=g", hash);
                  }
                  if (messageAuthor == null)
                  {
                      ViewBag.GravatarImageAlt = item.PostMessage_Author;
                      ViewBag.GravatarImageUrl = "http://s19.postimage.org/62du01yov/default_avatar_82.png";
                  }               
                <tr class="postmessage_border">
                    <td class="postmessage_Author center" style="width: 15%;">
                        @if (messageAuthor != null)
                        {
                            <img src='@String.Format("/Images/Flags/{0}.png", messageAuthor.Culture.Substring(3, 2))' />
                        }
                        else
                        {
                            <img src="/Images/Flags/fr.png" />                            
                        }
                        @Html.DisplayFor(modelItem => item.PostMessage_Author)
                        <br />
                        <br />
                        <img src="@ViewBag.GravatarImageUrl" alt="@ViewBag.GravatarImageAlt" style="padding: 0;margin-bottom:0px; border-radius: 8px;" />
                        <br />
                        <br />

                        @if (messageAuthor != null)
                        {
                            @CloudFranceTriple.WebRole.Properties.Resources.LoginRegisterCreationDate
                            <br />
                            @WebSecurity.GetCreateDate(item.PostMessage_Author).ToShortDateString()
                            <br />
                            @messageAuthor.ForumMessagePostedCounter
                        }
                        else
                        {
                            @Html.Raw("0 ")  
                        }
                        @CloudFranceTriple.WebRole.Properties.Resources.ForumMessagePostedCounter
                        <br />
                        <br />
                    </td>
                    <td class="postmessage_message">
                        <table style="height: 100%;">
                            <tr style="height: 5%;">
                                <td>@Html.DisplayFor(modelItem => item.PostMessage_CreationDate, "TemplateUserDateTime")</td>
                            </tr>
                            <tr style="height: 95%;">
                                <td>@Html.DisplayTextFor(modelItem => item.PostMessage_Message)</td>
                            </tr>
                            <tr style="height: 5%;">
                                <td>
                                    @if ((ViewBag.ShowCreate as Boolean?) ?? false)
                                    {
                                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonQuote, "Create", "Post", new { area = "Forum", id = (ViewBag.Topic_Id as int?) ?? 0, id2 = item.Id }, null);
                                    }
                                    @if (((ViewBag.IsUnLocked as Boolean?) ?? false) && (User.IsInRole("RoleAdminForum") || User.Identity.Name.ToLower() == item.PostMessage_Author.ToLower()))
                                    {
                                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonEdit, "Edit", new { id = item.Id }); 
                                        @Html.ActionLink(CloudFranceTriple.WebRole.Properties.Resources.ButtonDelete, "Delete", new { id = item.Id });
                                    }
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
              }
            }
        </table>
        @if ((ViewBag.ShowCreate as Boolean?) ?? false)
        {
            <p>
                @Html.ActionLink(@CloudFranceTriple.WebRole.Properties.Resources.PostCreateButton, "Create", "Post", new { area = "Forum", id = ViewBag.Topic_Id as int? }, null)
            </p>
        }
        @Html.Partial("_TopicQueriesLinks")
    </section>
    <h5>@Html.ActionLink(ViewBag.SiteMapPath1 as string, "Index", "Forum", new { area = "Forum" }, null) > @Html.ActionLink(ViewBag.SiteMapPath2 as string, "Index", "Topic", new { area = "Forum", id = ViewBag.Category_Id as int? }, null) > @ViewBag.SiteMapPath3 > @ViewBag.LocalizedUserRights</h5>
</div>
